package com.javatop.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.javatop.mapper.article.ArticleMapper;
import com.javatop.pojo.entity.Article;
import com.javatop.pojo.entity.Draft;
import com.javatop.pojo.entity.PageBean;
import com.javatop.service.serviceInterface.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/*
负责人：
功能：Service层，逻辑处理的实现
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    /*
负责人：王耀鹏
功能：根据页码和页数分页查找
参数：page,pageSize
返回值：PageBean

*/
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Article> articleList = articleMapper.list();
        Page<Article> p = (Page<Article>) articleList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /*
负责人：王耀鹏
功能：根据id查找
参数：id
返回值：Draft

*/
    @Override
    public Draft searchById(Integer id) {
        Draft draft = articleMapper.searchContent(id);
        return draft;
    }
    /*
 负责人：王耀鹏
 功能：根据文章内容分页查找
 参数：content,page,pageSize
 返回值：PageBean

  */
    @Override
    public PageBean searchByContent(String content, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Article> articleContent = articleMapper.content(content);
        Page<Article> p = (Page<Article>) articleContent;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /*
 负责人：王耀鹏
 功能：根据作者姓名分页查找
 参数：authorName,page,pageSize
 返回值：PageBean

  */
    @Override
    public PageBean searchByAnthor(String authorName, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Article> articleAnthor = articleMapper.anthor(authorName);
        Page<Article> p = (Page<Article>) articleAnthor;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /*
    负责人：
    功能：处理删除,修改，新增文章的逻辑处理的方法
    参数：
    返回值：void
     */
    @Override
    public void deleteArticle(int id) {

        articleMapper.deleteByKey(id);
    }

    @Override
    public void addArticle(Article article) {
        article.setReleaseTime(Timestamp.valueOf(LocalDateTime.now()));
        article.setLikeNumber(0);
        article.setSaveNumber(0);

        articleMapper.addByBody(article);
    }

    @Override
    public void modifyArticle(Article article) {

        articleMapper.modifyByBody(article);
    }
}
